package com.mssh.login.controller;

import com.mssh.entity.pojo.Result;
import com.mssh.login.dto.LoginDto;
import com.mssh.login.service.AuthService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 熳殊沙华
 * @Date: 2025/03/25/12:20
 * @Description:
 */
@RestController
@RequestMapping("/auth")
public class AuthController {

    private final static Logger logger = LogManager.getLogger(AuthController.class);

    @Autowired
    private AuthService authService;

    /**
     * 登录
     * @param dto
     * @return
     */
    @PostMapping("/login")
    public Result login(@RequestBody LoginDto dto) {
        Result result = new Result();
        try{
            result = authService.login(dto);
        }catch (Exception e){
            logger.warn("账号[{}]登录异常:", dto.getAccount(), e);
            result.failed("登录异常！");
        }
        return result;
    }

    /**
     * 生成验证码图片
     * @param request
     * @param response
     */
    @GetMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response){
        try{
            authService.captcha(request, response);
        }catch (Exception e){
            logger.warn("生成验证码图片异常:", e);
        }
    }


    // 需要权限的接口示例
    @GetMapping("/user")
    @RequiresPermissions("user:delete")
    public Result getUserInfo() {
        return new Result().success("user data");
    }
}
